﻿#include "rolecontroller.h"

#include <QJsonDocument>
#include <responesutils.h>
#include <roleservice.h>

RoleController::RoleController(QObject *parent) : HttpRequestHandler(parent)
{

}

RoleController::~RoleController()
{

}

void RoleController::service(HttpRequest &request, HttpResponse &response)
{
    QString path = request.getPath();
    QStringList pathList = path.split("/");
    if (pathList.size() < 3)
    {
        return this->returnError(response);
    }
    QString method = pathList.value(3);
    QByteArray responseMsg;
    QString methodType = request.getMethod();
    if (methodType == "GET")
    {
        if (method == "pagelist")
        {
            return getPageList(request, response);
        }else if (method == "list")
        {
            return getList(request, response);
        }
    }
    else if (methodType == "POST")
    {
        if (method == "save")
        {
            return saveOne(request, response);
        }
    }
    else if (methodType == "DELETE")
    {
        if (method == "delete")
        {
            return deleteOne(request, response);
        }
    }
    this->returnError(response);
}

void RoleController::getPageList(HttpRequest &request, HttpResponse &response)
{
    qDebug() << __FUNCTION__;
    uint64_t page = request.getParameter("page").toULongLong();
    uint64_t pageSize = request.getParameter("pageSize").toULongLong();
    if(page == 0 || pageSize == 0){
        return ResponesUtils::erroResponse(response,"page and pageSize and  are required");
    }
    RoleService objectService;
    uint64_t total=0;
    QList<Role> list;
    if(!objectService.getPageList(total,list,page,pageSize)){
          return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    QJsonArray jsonArray ;
    for(Role &item:list){
        jsonArray.append(item.toJsonObject());
    }
    return ResponesUtils::pagedResultResponse(response,total,jsonArray);
}

void RoleController::getList(HttpRequest &request, HttpResponse &response)
{
    RoleService objectService;
    QList<Role> list;
    if(!objectService.getList(list)){
          return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    QJsonArray jsonArray ;
    for(Role &item:list){
        jsonArray.append(item.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void RoleController::saveOne(HttpRequest &request, HttpResponse &response)
{
    QByteArray body = request.getBody();
    QJsonParseError parseError;
    QJsonDocument  jsonDocument= QJsonDocument::fromJson(body, &parseError);

    if (parseError.error != QJsonParseError::NoError)
    {
        return ResponesUtils::erroResponse(response,"请求参数JSON结构异常!");
    }
    Role object;
    qDebug()<<jsonDocument.object().value("params").toObject();
    object.fromJsonObject(jsonDocument.object().value("params").toObject());
    RoleService objectService;
    if(!objectService.saveOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,"添加成功！");
}

void RoleController::deleteOne(HttpRequest &request, HttpResponse &response)
{
    uint64_t id = request.getParameter("id").toULongLong();
    if(id == 0 ){
        return ResponesUtils::erroResponse(response,"请指定删除ID！");
    }
    Role object;
    object.setId(id);
    RoleService objectService;
    if(!objectService.deleteOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,"删除成功！");
}
